<?php

/**
 * @copyright Copyright 2012 CrApache-GUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 */

include 'edit_settings.php';

function change_port($port){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^Listen ([0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]))|(^\sListen ([0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]))/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "Listen $port\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function add_port($port){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $chk = 0;
    $pattern = '/(Listen ([0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]))|(^\sListen ([0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]))/';

    while(!feof($handle)){

        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1 && $chk == 0){
            $line .= "Listen $port\n";
            $chk = 1;
        }

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function delete_port($port){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/^Listen '.$port.'/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function change_server_root($root){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^ServerRoot)|(^\sServerRoot)|(^#ServerRoot)/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "ServerRoot $root\n\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function change_server_admin($admin){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^ServerAdmin)|(^\sServerAdmin)|(^#ServerAdmin)/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "ServerAdmin $admin\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function change_server_name($name){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^ServerName)|(^\sServerName)|(^#ServerName)/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "ServerName $name\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function change_document_root($root){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^DocumentRoot)|(^\sDocumentRoot)|(^#DocumentRoot)/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "DocumentRoot $root\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

function change_error_log($log){

    $conf = get_conf_original_location();
    $handle = fopen($conf, "r+");
    $file = " ";
    $pattern = '/(^ErrorLog)|(^\sErrorLog)|(^#ErrorLog)/';

    while(!feof($handle)){
        $line = fgets($handle);
        if(preg_match($pattern, $line) == 1)
            $line = "ErrorLog $log\n";

        $file .= $line;
    }

    rewind($handle);
    fwrite($handle, $file);
    fclose($handle);
}

//change_server_name("test");
?>
